//
//  GRContactInfomationViewController.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRContactInfomationViewController.h"
#import "GRCertificationHeader.h"
#import "GRCertificationFooter.h"
#import "GRCertificationViewCell.h"

@interface GRContactInfomationViewController ()

{
    NSArray *titleArray;
}
@property (nonatomic, strong) NSMutableArray *contactList;

@end

@implementation GRContactInfomationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    titleArray = @[@[@"直系亲属", @"真实姓名", @"联系号码"], @[@"重要朋友", @"真实姓名", @"联系号码"]];
    // Uncomment the following line to preserve selection between presentations.
    GRCertificationHeader *header  = [[GRCertificationHeader alloc] initWithCertification:GRCertificationContact];
    self.tableView.tableHeaderView = header;

    GRCertificationFooter *footer  = [[GRCertificationFooter alloc] initWithFrame:CGRectMake(0, 0, KscreenWidth, 65)];
    [footer.sureBtn setTitle:@"已认证" forState:UIControlStateNormal];
    self.tableView.tableFooterView = footer;

    self.tableView.backgroundColor = HEXCOLOR(0xf8f8f8);
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    [self.tableView registerNib:[UINib nibWithNibName:@"GRCertificationViewCell" bundle:nil] forCellReuseIdentifier:@"GRCertificationViewCell"];
    
    [self getContactsList];
}


- (void)getContactsList {
    [GRCertificationAction getALLContacts:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            for (NSDictionary *dic in responseObject) {
                NSMutableArray *array = [NSMutableArray array];
                NSString *type = [self getRelationWithType:[dic[@"type"] integerValue]];
                [array addObject:type];
                [array addObject:dic[@"name"]];
                [array addObject:dic[@"phone"]];
                [self.contactList addObject:array];
            }
            [self.tableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.contactList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRCertificationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GRCertificationViewCell" forIndexPath:indexPath];
    NSArray *titleArr       = titleArray[indexPath.section];
    cell.titleLabel.text    = titleArr[indexPath.row];
    
    NSArray *contacts       = self.contactList[indexPath.section];
    cell.infoTextField.text = contacts[indexPath.row];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? 0 : 10;
}

- (NSString *)getRelationWithType:(NSInteger)type {
    switch (type) {
        case 0:
            return @"父母";
            break;
        
        case 1:
            return @"配偶";
            break;

        case 2:
            return @"兄弟姐妹";
            break;

        case 3:
            return @"同事";
            break;

        case 4:
            return @"朋友";
            break;

        default:
            return @"同乡";
            break;
    }
}


-(NSMutableArray *)contactList {
    if (!_contactList) {
        _contactList = [NSMutableArray array];
    }
    return _contactList;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
